<?php

namespace User\Controller;

use Think\Controller;

class UserController extends Controller {

    public function index() {

        $whereStr = 'id=\'' . $_COOKIE['a'] . '\'';
        $user = M('User');
        $data = $user->where($whereStr)->select();


        $etc['head'] = '/images/AK.png';
        $etc['waitcommonNum'] = '0';
        $etc['honour'] = '敬请期待';

        $task = M('Task');

        //发布历史
        $whereStr = 'sendUserID=\'' . $_COOKIE['a'] . '\'';
        $etc['issueNum'] = $task->where($whereStr)->count();
        $issuehis = $task->where($whereStr)->limit(5)->select();
        $i = count($issuehis) - 1;
        for (; $i >= 0; $i--) {
            if ($issuehis[$i]['status'] == 0) {
                $issuehis[$i]['status'] = '未接';
            } elseif ($issuehis[$i]['status'] == 1) {
                $issuehis[$i]['status'] = '已接';
            } elseif ($issuehis[$i]['status'] == 2) {
                $issuehis[$i]['status'] = '完成';
            } elseif ($issuehis[$i]['status'] == 3) {
                $issuehis[$i]['status'] = '关闭';
            } else {
                $issuehis[$i]['status'] = '异常';
            }
        }

        //解决历史
        $whereStr = 'recvUserID=\'' . $_COOKIE['a'] . '\'';
        $etc['solveNum'] = $task->where($whereStr)->count();
        $solvehis = $task->where($whereStr)->limit(5)->select();
        $i = count($solvehis) - 1;
        for (; $i >= 0; $i--) {
            if ($solvehis[$i]['status'] == 0) {
                $solvehis[$i]['status'] = '未接';
            } elseif ($solvehis[$i]['status'] == 1) {
                $solvehis[$i]['status'] = '已接';
            } elseif ($solvehis[$i]['status'] == 2) {
                $solvehis[$i]['status'] = '完成';
            } elseif ($solvehis[$i]['status'] == 3) {
                $solvehis[$i]['status'] = '关闭';
            } else {
                $solvehis[$i]['status'] = '异常';
            }
        }
        
        //公告
        $news = "";

        //FAQ
        $FAQ = "";

        $this->assign('issuehis', $issuehis);
        $this->assign('solvehis', $solvehis);
        $this->assign('etc', $etc);
        $this->assign('sendUserID', $sendUserID);
        $this->assign('recvUserID', $recvUserID);
        $this->assign('data', $data);
        $this->display('index');
    }

    public function login() {
        $this->display('login');
    }

    public function modify() {
        $this->display('modify');
    }

    public function doModify() {
        if (empty($_POST['password']) || empty($_POST['oldPassword'])) {
            echo '信息异常!';
            return;
        }

        $newpassword['password'] = $_POST['password'];
        $oldPassword = $_POST['oldPassword'];

        $user = M('User');
        $user->where('id=' . $_COOKIE['a'])->save($newpassword);


        $data = '{"h":"' . C('USERCENTER') . '"}';
        echo $data;
    }

    public function allTask() {
        $title = '所有任务';

        $task = M('Task');
        $data = $task->select();

        $head = array(
            '任务标题',
            '发布时间',
            '赏金',
        );

        $this->assign('head', $head);
        $this->assign('title', $title);
        $this->assign('data', $data);
        $this->display('table');
    }

    //传入id  根据id查找所有此id账户下发布的所有任务
    public function allIssue() {
        $title = '发布历史';

        $task = M('Task');
        $whereStr = 'sendUserID=\'' . $_COOKIE['a'] . '\'';
        $data = $task->where($whereStr)->select();


        $head = array(
            '任务标题',
            '发布时间',
            '赏金',
        );

        $this->assign('head', $head);
        $this->assign('title', $title);
        $this->assign('data', $data);

        $this->display('table');
    }

    public function allSolve() {
        $data['pageTitle'] = '接单历史';


        $task = M('Task');
        $whereStr = 'recvUserID=\'' . $_COOKIE['a'] . '\'';
        $data = $task->where($whereStr)->select();


        $head = array(
            '任务标题',
            '发布时间',
            '赏金',
        );

        $this->assign('head', $head);
        $this->assign('title', $title);
        $this->assign('data', $data);

        $this->display('table');
    }

    public function allFAQ() {
        $title = '常见问题';

        $this->assign('title', $title);

        $this->display('table');
    }

    public function allNews() {
        $title = '最新要闻';

        $this->assign('title', $title);

        $this->display('table');
    }

    public function nologin() {
        $this->success('亲 , 你还没有登录哦!<br/><a href="' . C('LOGIN') . '">点击这里立即跳转</a>', C('LOGIN'), 3);
    }

    public function loginCheck() {
        $username = $_POST["username"];
        $password = $_POST["password"];
        $whereStr = 'username=\'' . $username . '\'';
        $remember = $_POST["remember"];

        $User = M('User');
        $data = $User->where($whereStr)->select();
        if ($data[0] == null) {
            $data = "用户不存在!";
        } else {
            $password = md5($password);

            if ($password == $data[0]['password']) {
                $this->setCookies($remember, $data[0]['ID']);
                $data = '{"h":"' . C('USERCENTER') . '"}';
            } else {
                $data = '密码不正确!';
            }
        }
        echo $data;
    }

    public function setCookies($remember, $ID) {
        $c = md5(md5($ID) . md5($_POST["time"] . $ID) . md5($_POST["time"]));
        if ($remember == "true") {
            setcookie('a', $ID, time() + 7 * 24 * 3600, '/');
            setcookie('b', $_POST["time"], time() + 7 * 24 * 3600, '/');
            setcookie('c', $c, time() + 7 * 24 * 3600, '/');
        } else {
            setcookie('a', $ID, 0, '/');
            setcookie('b', $_POST["time"], 0, '/');
            setcookie('c', $c, 0, '/');
        }
    }

    public function issueFind() {
        $this->display('issueFind');
    }

    public function doFind() {
        $key = $_GET['keyword'];
        if (empty($key)) {
            echo '关键字不能为空!';
            return;
        }

        $key . trim($key);

        $keyword = explode(' ', $key);

        $Task = M('Task');
        $Title = $Task->where("title like '%" . $keyword[0] . "%'")->limit(50)->select();
        $content = $Task->where("content like '%" . $keyword[0] . "%'")->limit(50)->select();

        $str = '{';
        $str .= '"title":"<table class=\'table table-striped\'><tr><th>标题</th><th>内容</th><th>发布时间</th><th>赏金</th><th>详细</th></tr>';
        if ($Title != null) {
            $str .= $this->makeTaskTable($Title);
        }
        $str .= '</table>",';
        $str .= '"content":"<table class=\'table table-striped\'><tr><th>标题</th><th>内容</th><th>发布时间</th><th>赏金</th><th>详细</th></tr>';
        if ($content != null) {
            $str .= $this->makeTaskTable($content);
        }
        $str .= '</table>"';
        $str .= '}';
        echo $str;
    }

    public function makeTaskTable($arr) {
        while (list(, $v) = each($arr)) {
            $str .= '<tr><td>' . $v['title'] . '</td><td>' . $v['content'] . '</td><td>' . $v['sendTime'] . '</td><td>' . $v['reward'] . "</td><td><a href='" . C('TASKINFO') . '/id/' . $v['ID'] . "'>详细</a></td></tr>";
        }
        return $str;
    }

    public function issueRequire() {
        $this->display('issueRequire');
    }

    public function doRequire() {

        if (empty($_GET['title']) || empty($_GET['content'])) {
            echo '信息异常！';
            return;
        }

        $task = M('Task');

        $task->create();
        $task->title = $_GET['title'];
        $task->content = $_GET['content'];
        $task->reward = $_GET['reward'];
        $task->sendUserID = $_COOKIE['a'];
        $task->status = 0;
        $task->sendTime = date('Y-m-d H:i:s', time());
        $d = $task->add();

        $data = '{"h":"' . C('TASKINFO') . '/id/' . $d . '"}';
        echo $data;
    }

    public function regist() {
        $this->display('regist');
    }

    public function doRegist() {
        $User = M('User');

        //检测用户是否已存在
        $whereStr = "username=\'" . $_POST['name'] . '\'';

        $data = $User->where($whereStr)->select();

        if ($data[0] != null) {
            $data = "用户已存在!";
            echo $data;
            return;
        }

        //新建用户
        $User->create();
        $User->username = $_POST['name'];
        $User->password = md5($_POST['pwd']);
        $ID = $User->add();


        //写cookie
        $this->setCookies('false', $ID);
        $data = '{"h":"' . C('USERCENTER') . '"}';
        echo $data;
    }

    public function taskInfo() {
        $whereStr = 'id=\'' . $_GET['id'] . '\'';
        $task = M('Task');
        $data = $task->where($whereStr)->select();

        $user = cookie('a');

        if ($data[0]['recvUserID'] != -1 || $user == $data[0]['sendUserID']) {
            $isable = "disabled='disabled'";
            $this->assign("isable", $isable);
        }

        if ($user != $data[0]['recvUserID'] && $user != $data[0]['sendUserID']) {
            $data[0]['content'] = "对不起,您还没有接下该任务,没有权限查看该任务详细内容";
        }

        $whereStr = 'id=\'' . $data[0]['sendUserID'] . '\'';
        $User = M('User');
        $sendUser = $User->where($whereStr)->select();

        $whereStr = 'id=\'' . $data[0]['recvUserID'] . '\'';
        $recvUser = $User->where($whereStr)->select();

        $this->assign("data", $data);
        $this->assign("send", $sendUser);
        $this->assign("recv", $recvUser);

        $this->display('taskInfo');
    }

    public function doRecv() {

        $whereStr = 'id=\'' . $_GET['ID'] . '\'';

        $task = M('Task');
        $data['recvUserID'] = $_COOKIE['a'];
        $data['status'] = 1;
        $task->where($whereStr)->save($data);
    }

    public function logout() {
        setcookie('a', '', -1, '/');
        $this->display('logout');
    }
    
    public function getwyucookie(){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://jwc.wyu.cn/student/');
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //$cookie_j = tempnam('/','cookie');
        //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_j);
        //curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_j);
        $rs = curl_exec($ch);
        curl_close($ch);
    }

}
